package com.lnsf.bookstore.system.category.controller;

import com.lnsf.bookstore.system.category.service.CategoryService;
import com.lnsf.bookstore.system.category.vo.Category;
import com.lnsf.bookstore.common.dto.ResultFailDto;
import com.lnsf.bookstore.common.dto.ResultSuccessDto;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/category")
public class CategoryController {

	@Autowired
    private CategoryService categoryService;

    @RequestMapping("/v_list.do")
    public String list(HttpServletRequest request, ModelMap modelMap){

        System.out.println("category.....");
        return "admin/category/category";
    }

    /**
     * 查看所有列表
     */
    @RequestMapping("/queryAll")
    @ResponseBody
    public Object queryAll(@RequestParam Map<String, Object> params) {
        List<Category> list = categoryService.selectAll();
       /* //添加顶级菜单
        ApiCategory root = new ApiCategory();
        root.setId(0);
        root.setName("一级分类");
        root.setParentCode("-1");
        root.setOpen(true);
        list.add(root);*/
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("list",list);
        return map;
    }

    /**
     * 查看信息
     */
    @RequestMapping("/info/{id}")
    @ResponseBody
    //@RequiresPermissions("category:info")
    public Object info(@PathVariable("id") Integer id) {
        Category category = categoryService.queryObject(id);
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("category",category);
        return map;
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    @ResponseBody
    //@RequiresPermissions("category:info")
    public Object delete(@RequestBody Integer[] ids) {
        if (categoryService.deleteBatch(ids) > 0) {
            return new ResultSuccessDto("删除成功");
        }else {
            return  new ResultFailDto("删除失败");
        }
    }


    /**
     * 修改、新增操作
     */
    @RequestMapping(value = "/saveOrUpdate", method = RequestMethod.POST)
    @ResponseBody
    public Object saveOrUpdate(HttpServletRequest request,Integer id ,@RequestBody Category category, ModelMap modelMap){
        if(category.getId()==null){//新增
            //verifyForm(category);
            categoryService.save(category);
        }else{//修改
            //verifyForm(category);
            categoryService.update(category);
        }
        return new ResultSuccessDto("操作成功");
    }

    /**
     * 查询,选择select
     *
     * @return
     */
    @RequestMapping("/getCategorySelect")
    @ResponseBody
    public Object getCategorySelect() {
        Map<String, Object> map = new HashMap<>();
        Category category = new Category();
        /*map.put("parentCode", "");*/
        category.setParentCode("0");
        List<Category> list = categoryService.selectParent(category);
        //添加顶级菜单
        Category root = new Category();
        root.setId(0);
        root.setName("一级分类");
        root.setCode("0");
        root.setParentCode("-1");
        root.setOpen(true);
        list.add(root);
        map.put("list", list);
        System.out.println("aaaa");
        return map;
    }
	
}
